package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.AddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 普通用户相关接口
 *
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 添加地址接口")
public class AddressBookController {
    @Resource
    private AddressBookService addressBookService;
    @PostMapping
    @ApiOperation("新增地址")
    public void add(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.add(addressBookUpsertReqDTO);
    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO){
        PageResult<AddressBookResDTO> pageResult= addressBookService.page(addressBookPageQueryReqDTO);
        return pageResult;
    }
    @PutMapping("/{id}")
    @ApiOperation("地址薄修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址薄id", required = true, dataTypeClass = Long.class)
    })
    public void update(@NotNull(message = "id不能为空") @RequestParam("id") Long id,
                       @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO){
        addressBookService.update(id,addressBookUpsertReqDTO);
    }
    @DeleteMapping("/batch")
    @ApiOperation("地址薄批量删除")
    @ApiImplicitParam(name = "ids", value = "地址薄id列表", required = true, dataTypeClass = List.class)
    public void logicallyDelete(@NotNull(message = "id不能为空") @RequestBody List<Long> ids){
        System.out.println(ids);
        addressBookService.removeByIds(ids);
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO defaultAddress(){
        return addressBookService.defaultAddress();
    }
    @GetMapping("/{id}")
    @ApiOperation("地址薄详情")
    @ApiImplicitParams(
            @ApiImplicitParam(name="id",value = "地址薄id",required = true,dataTypeClass = Long.class)
    )
    public AddressBookResDTO detail(@NotNull(message="id不能为空")@RequestParam("id") long id){
        AddressBook addressBook =  addressBookService.getById(id);
        return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }

    @PutMapping("/default")
    @ApiOperation("地址薄设为默认/取消默认")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "地址薄id",required = true,dataTypeClass = Long.class),
            @ApiImplicitParam(name="flag",value = "是否为默认地址1:是，0不是",required = true,dataTypeClass = Integer.class)
    })
    public void updateDefaultStatus(@NotNull(message="id不能为空") @RequestParam("id") long id,
                                     @NotNull(message = "状态值不能为空状态值不能为空") @RequestParam("flag") Integer flag){
        Long aLong = UserContext.currentUserId();
        addressBookService.updateDefaultStatus(aLong,id,flag);
    }






}
